using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Reflection;
using System.Diagnostics;

namespace NETColorsExplorer
{
    public partial class MainForm : Form
    {
        private List<ColorInfo> _colorList = new List<ColorInfo>();

        public MainForm()
        {
            InitializeComponent();
            this.DoubleBuffered = true;

            Console.WriteLine("Company: " + Application.CompanyName);
            Console.WriteLine("App Name: " + Application.ProductName);
            this.toolTip1.SetToolTip(this.linkAbout, string.Format(
@"{0} {1}
{2}

Visit the official site for more information about the tool.",
Application.ProductName,
AssemblyVersion,
AssemblyCopyright));

            List<ColorsProvider> list = new List<ColorsProvider>(new ColorsProvider[]{
                new SystemColorsProvider() ,
                new KnownColorsProvider() ,
                new StandardColorsProvider()
            });
            cmbColorsProvider.DataSource = list;
            cmbColorsProvider.DisplayMember = "Name";

            cmbColorsProvider.SelectedIndex = 0;

            canva.Image = new Bitmap(canva.Size.Width - 2, canva.Size.Height - 2);
            canva.Paint += new PaintEventHandler(canva_Paint);
        }

        private string AssemblyCopyright
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
                if (attributes.Length == 0)
                    return string.Empty;
                return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
            }
        }

        private string AssemblyVersion
        {
            get
            {
                return Assembly.GetExecutingAssembly().GetName().Version.ToString(2);
            }
        }


        private void canva_Paint(object sender, PaintEventArgs e)
        {
            int width = 50, height = 20;
            int maxX = canva.Image.Size.Width;
            int maxY = canva.Image.Size.Height;
            int x = 2, y = 2;

            _colorList.Clear();
            for (int i = 0; i < CurrentColorsProvider.Count; i++)
            {
                Color color = CurrentColorsProvider[i];
                Rectangle rect = new Rectangle(x, y, width, height);
                e.Graphics.FillRectangle(new SolidBrush(color), rect);
                ControlPaint.DrawBorder(e.Graphics, rect, SystemColors.ActiveBorder, ButtonBorderStyle.Solid);

                rect.Inflate(2, 2);
                if (SelectedColorIndex == i)
                    ControlPaint.DrawBorder(e.Graphics, rect, SystemColors.Highlight, ButtonBorderStyle.Solid);
                else
                    if (CurrentColorIndex == i)
                        ControlPaint.DrawFocusRectangle(e.Graphics, rect);


                ColorInfo info = new ColorInfo(rect, color);
                _colorList.Add(info);

                if (x + width < maxX - width)
                    x += width + 3;
                else
                {
                    y += height + 3;
                    x = 2;
                }
            }
        }

        private ColorsProvider CurrentColorsProvider
        {
            get { return cmbColorsProvider.SelectedItem as ColorsProvider; }
        }

        private int _currentColorIndex = -1;

        public int CurrentColorIndex
        {
            get { return _currentColorIndex; }
            set
            {
                _currentColorIndex = value;

                if (_colorList.Count > 0)
                {
                    if (_currentColorIndex != -1)
                        ShowColorDetails(_colorList[_currentColorIndex].Color);
                    else if (SelectedColorIndex != -1)
                        ShowColorDetails(_colorList[SelectedColorIndex].Color);
                    else
                        ShowColorDetails(Color.Empty);
                }
                else
                    ShowColorDetails(Color.Empty);
            }
        }

        private void ShowColorDetails(Color color)
        {
            if (color != Color.Empty)
            {
                txtName.Text = color.Name;
                int d = color.R << 16;
                d += color.G << 8;
                d += color.B;
                txtHex.Text = d.ToString("X");
            }
            else
            {
                txtHex.Text = txtName.Text = string.Empty;
            }
        }

        private int _selectedColorIndex = -1;

        public int SelectedColorIndex
        {
            get { return _selectedColorIndex; }
            set
            {
                _selectedColorIndex = value;

                if (_colorList.Count > 0 && _selectedColorIndex != -1)
                    ShowColorDetails(_colorList[_selectedColorIndex].Color);
            }
        }

        private void canva_MouseMove(object sender, MouseEventArgs e)
        {
            if (_colorList.Count > 0 && (CurrentColorIndex == -1 || _colorList[CurrentColorIndex].Rectangle.Contains(e.Location) == false))
            {
                for (int i = 0; i < _colorList.Count; i++)
                {
                    ColorInfo info = _colorList[i];
                    if (info.Rectangle.Contains(e.Location))
                    {
                        CurrentColorIndex = i;
                        canva.Invalidate();
                        toolTip1.Show(string.Format("{0}", info.Color.Name), canva);
                        return;
                    }
                }
                CurrentColorIndex = -1;
                toolTip1.Hide(canva);
                canva.Invalidate();
            }
        }

        private void MainForm_Resize(object sender, EventArgs e)
        {
            canva.Image = new Bitmap(canva.Size.Width - 2, canva.Size.Height - 2);
            canva.Invalidate();
        }

        private void canva_MouseClick(object sender, MouseEventArgs e)
        {
            Rectangle rect;

            if (_colorList.Count > 0 && SelectedColorIndex != -1)
            {
                rect = new Rectangle(_colorList[SelectedColorIndex].Rectangle.Location, _colorList[SelectedColorIndex].Rectangle.Size);
                rect.Inflate(2, 2);
                canva.Invalidate(rect);
            }

            SelectedColorIndex = CurrentColorIndex;

            if (_colorList.Count > 0 && SelectedColorIndex != -1)
            {
                rect = new Rectangle(_colorList[SelectedColorIndex].Rectangle.Location, _colorList[SelectedColorIndex].Rectangle.Size);
                rect.Inflate(2, 2);
                canva.Invalidate(rect);
            }
        }

        private void cmbColorsProvider_SelectedIndexChanged(object sender, EventArgs e)
        {
            CurrentColorIndex = SelectedColorIndex = -1;
            canva.Invalidate();
        }

        private void linkAbout_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start("http://netcolorsexplorer.codeplex.com");
        }
    }
}